﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using ScrumTable.Common;
using ScrumTable.Common.Collections;
using ScrumTable.Common.Threading;

#endregion

namespace ScrumTable.Common.Monitoring
{
    /// <summary>
    /// This class is used to observe an import status. It can be implemented
    /// for different platforms and frameworks. This monitor will delegate all calls to a parent monitor
    /// and only propagate the finished event.
    /// </summary>
    public class CallbackDataImportMonitor : IDataImportMonitor
    {
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------

        private readonly AsyncCallback _callback;
        private string _identifier = null;
        private readonly IDataImportMonitor _decorator;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        /// <summary>
        /// Creates a new ActionDataImportMonitor instance with the specified data.
        /// </summary>
        /// <param name="callback">Specifies the callback method which is executed on the end.</param>
        /// <param name="decorator">delegates all call to this monitor</param>
        public CallbackDataImportMonitor(AsyncCallback callback, IDataImportMonitor decorator)
        {
            PreCondition.AssertNotNull(callback, "callback");
            _decorator = decorator;
            _callback = callback;
        }

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Calling this method determins that a new sub import progress has
        /// been started. This means that an additional progress bar should
        /// be displayed.
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        /// references to the ReportProgress / EndSubProgress methods.</param>
        public void StartSubProgress(string identifier)
        {
            _decorator.StartSubProgress(identifier);
            if (string.IsNullOrEmpty(_identifier))
                _identifier = identifier;
        }

        /// <summary>
        /// Reports a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        ///   references to the StartSubProgress / EndSubProgress methods.</param>
        /// <param name="current">Specifies the current state in percent. The maximum is 100.</param>
        public void ReportProgress(string identifier, byte current)
        {
            _decorator.ReportProgress(identifier, current);
        }

        /// <summary>
        /// Reports a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        ///   references to the StartSubProgress / EndSubProgress methods.</param>
        /// <param name="current">Specifies the current state.</param>
        /// <param name="max">Specifies the maximum.</param>
        public void ReportProgress(string identifier, int current, int max)
        {
            _decorator.ReportProgress(identifier, current, max);
        }

        /// <summary>
        /// Reports the end of a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        /// references to the StartSubProgress / ReportProgress methods.</param>
        /// <param name="e">This argument will contain an exception if the progress has been aborted.</param>
        public void EndSubProgress(string identifier, Exception e)
        {
            _decorator.EndSubProgress(identifier,e);
            if (identifier == _identifier)
            {
                _callback(new DataResult(e));
                _identifier = null;
            }
        }

        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------

        #endregion
    }
}
